package com.xiaoyi.intentsdklibrary.SDK.Action;

import android.accessibilityservice.AccessibilityServiceInfo;
import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Build;
import android.os.Handler;
import android.view.accessibility.AccessibilityManager;

import com.lmiot.xyclick.Util.LkxLog;
import com.xiaoyi.intentsdklibrary.Bean.PathBean;
import com.xiaoyi.intentsdklibrary.Bean.PointBean;
import com.xiaoyi.intentsdklibrary.Bean.TextBean;
import com.xiaoyi.intentsdklibrary.Bean.ViewBean;
import com.xiaoyi.intentsdklibrary.SDK.FloatView.NoteInfoViewSDK;
import com.xiaoyi.intentsdklibrary.SDK.SDK;
import com.xiaoyi.intentsdklibrary.SDK.Search.SearchSDK;
import com.xiaoyi.intentsdklibrary.StateBarUtil;
import com.youyi.yyviewsdklibrary.Dialog.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

public class ActionAsSDK {
    private static final ActionAsSDK ourInstance = new ActionAsSDK();
    private Accessibility mAccessibility;
    Context mContext;
    private Handler mHandler;
    private Intent mIntent;
    private boolean mResult;
    private int mScreenHeight;
    private int mScreenWith;

    public static ActionAsSDK getInstance() {
        return ourInstance;
    }

    private ActionAsSDK() {
    }

    public void init(Context context) {
        this.mContext = context;
        this.mAccessibility = Accessibility.getInstance();
        this.mScreenWith = SearchSDK.getInstance().getScreenWith(context);
        this.mScreenHeight = SearchSDK.getInstance().getScreenHeight(context);
    }

    public boolean checkAs(Context context) {
        String str = context.getPackageName() + "/com.xiaoyi.intentsdklibrary.SDK.Action.Accessibility";
        try {
            for (AccessibilityServiceInfo accessibilityServiceInfo : ((AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE)).getEnabledAccessibilityServiceList(16)) {
                if (accessibilityServiceInfo.getId().contains(str)) {
                    return true;
                }
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public void openAs(Context context) {
        try {
            Intent intent = new Intent("android.settings.ACCESSIBILITY_SETTINGS");
            this.mIntent = intent;
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(this.mIntent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void clickXY(final int i, final int i2) {
        if (SDK.isRunning) {
            try {
                Thread mThread = new Thread() {
                    public void run() {
                        if (ActionAsSDK.this.mAccessibility != null) {
                            LkxLog.d("点击位置x："+i+",y:"+i2);
                            ActionAsSDK.this.mAccessibility.clickXY(i, i2, 50);
                        }
                    }
                };
                mThread.start();
                mThread.join();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void doubleClickXY(int i, int i2) {
        if (SDK.isRunning) {
            try {
                clickXY(i, i2);
                clickXY(i, i2);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void longClickXY(final int i, final int i2) {
        if (SDK.isRunning) {
            try {
                Thread mThread = new Thread() {
                    /* class com.xiaoyi.intentsdklibrary.SDK.Action.ActionAsSDK.AnonymousClass2 */

                    public void run() {
                        if (ActionAsSDK.this.mAccessibility != null) {
                            ActionAsSDK.this.mAccessibility.clickXY(i, i2, 2000);
                        }
                    }
                };
                mThread.start();
                mThread.join();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void longClickXYTimes(final int i, final int i2, final int i3) {
        if (SDK.isRunning) {
            try {
                Thread mThread = new Thread() {
                    public void run() {
                        if (ActionAsSDK.this.mAccessibility != null) {
                            ActionAsSDK.this.mAccessibility.clickXY(i, i2, i3);
                        }
                    }
                };
                mThread.start();
                mThread.join();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public boolean performAction(Accessibility.ActionType actionType) {
        Accessibility accessibility;
        if (SDK.isRunning && (accessibility = this.mAccessibility) != null) {
            return accessibility.performAction(actionType);
        }
        return false;
    }

    public void swipeLeft(final int i) {
        if (SDK.isRunning) {
            final int i2 = this.mScreenWith - 100;
            int i3 = this.mScreenHeight;
            final int i4 = i3 / 2;
            final int i5 = i3 / 2;
            try {
                Thread mThread = new Thread() {
                    public void run() {
                        if (ActionAsSDK.this.mAccessibility != null) {
                            ActionAsSDK.this.mAccessibility.swipe(i2, i5, 100, i4, i);
                        }
                    }
                };
                mThread.start();
                mThread.join();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void swipeRight(final int i) {
        if (SDK.isRunning) {
            final int i2 = this.mScreenWith - 100;
            int i3 = this.mScreenHeight;
            final int i4 = i3 / 2;
            final int i5 = i3 / 2;
            try {
                Thread mThread = new Thread() {
                    public void run() {
                        if (ActionAsSDK.this.mAccessibility != null) {
                            ActionAsSDK.this.mAccessibility.swipe(100, i4, i2, i5, i);
                        }
                    }
                };
                mThread.start();
                mThread.join();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void swipeDown(final int i) {
        if (SDK.isRunning) {
            final int statusBarHeight = (this.mScreenHeight - 200) - StateBarUtil.getStatusBarHeight(this.mContext);
            final int statusBarHeight2 = StateBarUtil.getStatusBarHeight(this.mContext) + 200;
            int i2 = this.mScreenWith;
            final int i3 = i2 / 2;
            final int i4 = i2 / 2;
            try {
                Thread mThread = new Thread() {
                    public void run() {
                        if (ActionAsSDK.this.mAccessibility != null) {
                            ActionAsSDK.this.mAccessibility.swipe(i4, statusBarHeight2, i3, statusBarHeight, i);
                        }
                    }
                };
                mThread.start();
                mThread.join();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void swipeUp(final int i) {
        if (SDK.isRunning) {
            final int statusBarHeight = (this.mScreenHeight - 100) - StateBarUtil.getStatusBarHeight(this.mContext);
            final int statusBarHeight2 = StateBarUtil.getStatusBarHeight(this.mContext) + 100;
            int i2 = this.mScreenWith;
            final int i3 = i2 / 2;
            final int i4 = i2 / 2;
            try {
                Thread mThread = new Thread() {
                    public void run() {
                        if (ActionAsSDK.this.mAccessibility != null) {
                            ActionAsSDK.this.mAccessibility.swipe(i3, statusBarHeight, i4, statusBarHeight2, i);
                        }
                    }
                };
                mThread.start();
                mThread.join();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void swipeBigger(int i) {
        if (SDK.isRunning) {
            int i2 = this.mScreenWith / 2;
            int i3 = this.mScreenHeight / 2;
            final PathBean pathBean = new PathBean(i2, i3, i2 - 400, i3 + 400, i);
            final PathBean pathBean2 = new PathBean(i2, i3, i2 + 400, i3 - 400, i);
            try {
                Thread mThread =  new Thread() {
                    public void run() {
                        if (ActionAsSDK.this.mAccessibility != null) {
                            ActionAsSDK.this.mAccessibility.multiPath(pathBean, pathBean2);
                        }
                    }
                };
                mThread.start();
                mThread.join();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void swipeSmaller(int i) {
        if (SDK.isRunning) {
            int i2 = this.mScreenWith / 2;
            int i3 = this.mScreenHeight / 2;
            final PathBean pathBean = new PathBean(100, i3, i2, i3, i);
            final PathBean pathBean2 = new PathBean(this.mScreenWith - 100, i3, i2, i3, i);
            try {
                new Thread() {
                    /* class com.xiaoyi.intentsdklibrary.SDK.Action.ActionAsSDK.AnonymousClass9 */

                    public void run() {
                        if (ActionAsSDK.this.mAccessibility != null) {
                            ActionAsSDK.this.mAccessibility.multiPath(pathBean, pathBean2);
                        }
                    }
                }.start();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void clickViewID(final String str) {
        if (SDK.isRunning) {
            try {
                Thread mThread = new Thread() {
                    /* class com.xiaoyi.intentsdklibrary.SDK.Action.ActionAsSDK.AnonymousClass10 */

                    public void run() {
                        if (ActionAsSDK.this.mAccessibility != null) {
                            ActionAsSDK.this.mAccessibility.clickViewID(str);
                        }
                    }
                };
                mThread.start();
                mThread.join();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void clickViewIDByPosition(final String str, final int i) {
        if (SDK.isRunning) {
            try {
                Thread mThread = new Thread() {
                    public void run() {
                        if (ActionAsSDK.this.mAccessibility != null) {
                            ActionAsSDK.this.mAccessibility.clickViewIDByPosition(str, i);
                        }
                    }
                };
                mThread.start();
                mThread.join();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void clickMultiPoint(final List<PointBean> list) {
        if (SDK.isRunning) {
            try {
                Thread mThread = new Thread() {
                    public void run() {
                        if (ActionAsSDK.this.mAccessibility != null) {
                            ActionAsSDK.this.mAccessibility.clickMultiPoint(list);
                        }
                    }
                };
                mThread.start();
                mThread.join();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void longClickMultiPoint(final int i, final List<PointBean> list) {
        if (SDK.isRunning) {
            try {
                Thread mThread = new Thread() {
                    public void run() {
                        if (ActionAsSDK.this.mAccessibility != null) {
                            ActionAsSDK.this.mAccessibility.longClickMultiPoint(i, list);
                        }
                    }
                };
                mThread.start();
                mThread.join();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void pathList(final int i, final List<PointBean> list) {
        if (SDK.isRunning) {
            try {
                Thread mThread = new Thread() {
                    public void run() {
                        if (ActionAsSDK.this.mAccessibility != null) {
                            ActionAsSDK.this.mAccessibility.pathList(i, list);
                        }
                    }
                };
                mThread.start();
                mThread.join();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void drapTo(final PointBean pointBean, final PointBean pointBean2, final int i) {
        if (SDK.isRunning) {
            try {
                Thread mThread =new Thread() {
                    public void run() {
                        if (ActionAsSDK.this.mAccessibility != null) {
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                                ActionAsSDK.this.mAccessibility.dragTo(pointBean, pointBean2, i);
                            }else {
                                ToastUtil.err("当前安卓版本不支持");
                            }
                        }
                    }
                };
                mThread.start();
                mThread.join();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public List<TextBean> findAllText() {
        if (!SDK.isRunning) {
            return new ArrayList();
        }
        Accessibility accessibility = this.mAccessibility;
        if (accessibility != null) {
            return accessibility.findAllText();
        }
        return new ArrayList();
    }

    public String findAllText01() {
        StringBuffer stringBuffer = new StringBuffer();
        for (TextBean textBean : findAllText()) {
            stringBuffer.append(textBean.getText());
        }
        return stringBuffer.toString();
    }

    public void longClickText(final String str) {
        if (SDK.isRunning) {
            try {
                Thread  mThread = new Thread() {
                    public void run() {
                        if (ActionAsSDK.this.mAccessibility != null) {
                            ActionAsSDK.this.mAccessibility.clickText(str, null, true);
                        }
                    }
                };
                mThread.start();
                mThread.join();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void clickTextRect(final String str, final Rect rect) {
        if (SDK.isRunning) {
            try {
                Thread mThread = new Thread() {
                    public void run() {
                        if (ActionAsSDK.this.mAccessibility != null) {
                            ActionAsSDK.this.mAccessibility.clickText(str, rect, false);
                        }
                    }
                };
                mThread.start();
                mThread.join();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void inputTextByPosition(int i, String str) {
        Accessibility accessibility;
        if (SDK.isRunning && (accessibility = this.mAccessibility) != null) {
            accessibility.inputTextByPosition(i, str);
        }
    }

    public void inputTextByXY(int i, int i2, String str) {
        Accessibility accessibility;
        if (SDK.isRunning && (accessibility = this.mAccessibility) != null) {
            accessibility.inputTextByXY(i, i2, str);
        }
    }

    public void inputTextByRect(Rect rect, String str) {
        Accessibility accessibility;
        if (SDK.isRunning && (accessibility = this.mAccessibility) != null) {
            accessibility.inputTextByRect(rect, str);
        }
    }

    public void inputTextByForm0(String str) {
        if (SDK.isRunning) {
            ArrayList arrayList = new ArrayList();
            arrayList.add(str);
            Accessibility accessibility = this.mAccessibility;
            if (accessibility != null) {
                accessibility.inputTextByForm(arrayList);
            }
        }
    }

    public void inputTextByForm(List<String> list) {
        Accessibility accessibility;
        if (SDK.isRunning && (accessibility = this.mAccessibility) != null) {
            accessibility.inputTextByForm(list);
        }
    }

    public static void setSlipeRandom(Context context, boolean z) {
        context.getSharedPreferences("VoiceData_1", 0).edit().putBoolean("setSlipeRandom", z).commit();
    }

    public static boolean getSlipeRandom(Context context) {
        return context.getSharedPreferences("VoiceData_1", 0).getBoolean("setSlipeRandom", false);
    }

    public static void setWaitTime(Context context, boolean z) {
        context.getSharedPreferences("VoiceData_1", 0).edit().putBoolean("setWaitTime", z).commit();
    }

    public static boolean getWaitTime(Context context) {
        return context.getSharedPreferences("VoiceData_1", 0).getBoolean("setWaitTime", true);
    }

    public static void setDelayUnitMs(Context context, boolean z) {
        context.getSharedPreferences("VoiceData_1", 0).edit().putBoolean("setDelayUnitMs", z).commit();
    }

    public static boolean getDelayUnitMs(Context context) {
        return context.getSharedPreferences("VoiceData_1", 0).getBoolean("setDelayUnitMs", false);
    }

    public static void setShowAsButton(Context context, boolean z) {
        context.getSharedPreferences("VoiceData_1", 0).edit().putBoolean("getShowAsButton", z).commit();
    }

    public static boolean getShowAsButton(Context context) {
        return context.getSharedPreferences("VoiceData_1", 0).getBoolean("getShowAsButton", true);
    }

    public static void setCloseAllVibrate(Context context, boolean z) {
        context.getSharedPreferences("VoiceData_1", 0).edit().putBoolean("setCloseAllVibrateNew", z).commit();
    }

    public static boolean getCloseAllVibrate(Context context) {
        return context.getSharedPreferences("VoiceData_1", 0).getBoolean("setCloseAllVibrateNew", true);
    }

    public static void setCreateAutoNotic(Context context, boolean z) {
        context.getSharedPreferences("VoiceData_1", 0).edit().putBoolean("setCreateAutoNotic", z).commit();
    }

    public static boolean getCreateAutoNotic(Context context) {
        return context.getSharedPreferences("VoiceData_1", 0).getBoolean("setCreateAutoNotic", true);
    }

    public List<ViewBean> getAllNode(NoteInfoViewSDK.FindViewType findViewType) {
        Accessibility accessibility = this.mAccessibility;
        if (accessibility != null) {
            return accessibility.getAllNode(findViewType);
        }
        return null;
    }

    public void controlView(final ViewBean viewBean) {
        if (SDK.isRunning) {
            try {
                Thread mThread = new Thread() {

                    public void run() {
                        if (ActionAsSDK.this.mAccessibility != null) {
                            ActionAsSDK.this.mAccessibility.controlView(viewBean);
                        }
                    }
                };
               mThread .start();
               mThread.join();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public String findPackName() {
        Accessibility accessibility = this.mAccessibility;
        if (accessibility != null) {
            return accessibility.findPackName();
        }
        return null;
    }

    public String findActivityName() {
        Accessibility accessibility = this.mAccessibility;
        if (accessibility != null) {
            return accessibility.findActivityName();
        }
        return null;
    }

    public boolean findHasViewID(ViewBean viewBean) {
        Accessibility accessibility = this.mAccessibility;
        if (accessibility != null) {
            return accessibility.findHasViewID(viewBean);
        }
        return false;
    }

    public boolean findViewText(ViewBean viewBean) {
        Accessibility accessibility = this.mAccessibility;
        if (accessibility != null) {
            return accessibility.findViewText(viewBean);
        }
        return false;
    }

    public boolean findViewDes(ViewBean viewBean) {
        Accessibility accessibility = this.mAccessibility;
        if (accessibility != null) {
            return accessibility.findViewDes(viewBean);
        }
        return false;
    }

    public boolean findViewState(ViewBean viewBean) {
        Accessibility accessibility = this.mAccessibility;
        if (accessibility != null) {
            return accessibility.findViewState(viewBean);
        }
        return false;
    }

    public List<PointBean> findViewByIDAll(String str) {
        Accessibility accessibility;
        if (SDK.isRunning && (accessibility = this.mAccessibility) != null) {
            return accessibility.findViewByIDAll(str);
        }
        return null;
    }

    public boolean findHasViewIDByString(String str) {
        Accessibility accessibility = this.mAccessibility;
        if (accessibility != null) {
            return accessibility.findHasViewIDByString(str);
        }
        return false;
    }

    public void clickText(String str) {
        List<TextBean> findAllText = getInstance().findAllText();
        ArrayList arrayList = new ArrayList();
        for (TextBean textBean : findAllText) {
            if (textBean.getText().equals(str) || textBean.getText().contains(str)) {
                arrayList.add(textBean);
            }
        }
        if (arrayList.size() > 0) {
            TextBean textBean2 = (TextBean) arrayList.get(0);
            getInstance().clickXY(textBean2.getClickX(), textBean2.getClickY());
        }
    }

    public void clickTextNum(String str, int i) {
        List<TextBean> findAllText = getInstance().findAllText();
        ArrayList arrayList = new ArrayList();
        for (TextBean textBean : findAllText) {
            if (textBean.getText().equals(str) || textBean.getText().contains(str)) {
                arrayList.add(textBean);
            }
        }
        if (arrayList.size() > 0) {
            int i2 = i - 1;
            if (i2 < 0) {
                i2 = 0;
            }
            if (arrayList.size() > i) {
                TextBean textBean2 = (TextBean) arrayList.get(i2);
                getInstance().clickXY(textBean2.getClickX(), textBean2.getClickY());
                return;
            }
            TextBean textBean3 = (TextBean) arrayList.get(0);
            getInstance().clickXY(textBean3.getClickX(), textBean3.getClickY());
        }
    }

    public void clickXYDureation(final int i, final int i2, final int i3) {
        if (SDK.isRunning) {
            try {
                Thread  mThread  = new Thread() {

                    public void run() {
                        if (ActionAsSDK.this.mAccessibility != null) {
                            ActionAsSDK.this.mAccessibility.clickXY(i, i2, i3);
                        }
                    }
                };
                mThread.start();
                mThread.join();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void clickTextLastNum(String str) {
        List<TextBean> findAllText = getInstance().findAllText();
        ArrayList arrayList = new ArrayList();
        for (TextBean textBean : findAllText) {
            if (textBean.getText().equals(str) || textBean.getText().contains(str)) {
                arrayList.add(textBean);
            }
        }
        if (arrayList.size() > 0) {
            TextBean textBean2 = (TextBean) arrayList.get(arrayList.size() - 1);
            getInstance().clickXY(textBean2.getClickX(), textBean2.getClickY());
        }
    }
}
